// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DBTickSizeRuleHelpers.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge.Entities
{
    #region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Org.OpenTrader.Framework.Entities;
    using Org.OpenTrader.Framework.Forge.Interfaces;

    #endregion

    /// <summary>
    /// The db tick size rule helpers.
    /// </summary>
    public partial class DBTickSizeRuleHelpers
    {
        #region Public Methods

        /// <summary>
        /// The create tsr.
        /// </summary>
        /// <param name="dictionaryId">
        /// The dictionaryId.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <returns>
        /// </returns>
        public static ITickSizeRule CreateTSR(Guid dictionaryId, string name)
        {
            var dbDictionary = DBDictionaryServiceHelpers.GetDBDictionaryService(dictionaryId);
            if (dbDictionary != null)
            {
                var dbTsr = DBTickSizeRule.CreateDBTickSizeRule(Guid.NewGuid(), name);
                dbDictionary.DBTickSizeRule.Add(dbTsr);

                var dbTsrField1 = DBTickSizeRuleField.CreateDBTickSizeRuleField(Guid.NewGuid());
                dbTsrField1.Price = 0;
                dbTsrField1.TickSize = 0.01;
                dbTsr.DBTickSizeRuleField.Add(dbTsrField1);

                if (Singleton<DB>.Instance.Commit() > 0)
                {
                    return new TickSizeRule(dbTsr);
                }
            }

            return null;
        }

        /// <summary>
        /// The load tsr.
        /// </summary>
        /// <param name="dbDictionary">
        /// The dictionary id.
        /// </param>
        /// <param name="tickSizeRuleName">
        /// The tick size rule name.
        /// </param>
        /// <returns>
        /// </returns>
        public static DBTickSizeRule GetDBTickSizeRule(DBDictionaryService dbDictionary, string tickSizeRuleName)
        {
            var dbTsrs = from t in dbDictionary.DBTickSizeRule where t.Name == tickSizeRuleName select t;

            if (dbTsrs.Count() <= 0)
            {
                return null;
            }

            var tsr = dbTsrs.First();
            tsr.DBTickSizeRuleField.Load();
            return tsr;
        }

        /// <summary>
        /// The load tsr.
        /// </summary>
        /// <param name="tsrId">
        /// The tsrId.
        /// </param>
        /// <returns>
        /// </returns>
        public static ITickSizeRule GetTSR(Guid tsrId)
        {
            var dbTsrs = from t in Singleton<DB>.Instance.Entities.DBTickSizeRule where t.ID == tsrId select t;

            if (dbTsrs.Count() <= 0)
            {
                return null;
            }

            return new TickSizeRule(dbTsrs.First());
        }

        /// <summary>
        /// The load ts rs.
        /// </summary>
        /// <param name="dictionaryId">
        /// The dictionaryId.
        /// </param>
        /// <returns>
        /// </returns>
        public static ITickSizeRule[] GetTSRs(Guid dictionaryId)
        {
            var dbTsrs = from t in Singleton<DB>.Instance.Entities.DBTickSizeRule where t.DBDictionaryService.ID == dictionaryId select t;

            if (dbTsrs.Count() <= 0)
            {
                return new ITickSizeRule[] { };
            }

            IList<ITickSizeRule> tsrs = new List<ITickSizeRule>();
            foreach (var dbTsr in dbTsrs)
            {
                tsrs.Add(new TickSizeRule(dbTsr));
            }

            return tsrs.ToArray();
        }

        /// <summary>
        /// The save tsr.
        /// </summary>
        /// <param name="tsr">
        /// The tsr.
        /// </param>
        /// <returns>
        /// The save tsr.
        /// </returns>
        public static bool SaveTSR(ITickSizeRule tsr)
        {
            var dbTsrs = from t in Singleton<DB>.Instance.Entities.DBTickSizeRule where t.ID == tsr.ID select t;

            if (dbTsrs.Count() <= 0)
            {
                return false;
            }

            var dbTsr = dbTsrs.First();
            var dbDictionaryService = dbTsr.DBDictionaryService;

            // Delete the ticksize rule (and fields, because cascading is on)
            Singleton<DB>.Instance.Entities.DeleteObject(dbTsr);
            Singleton<DB>.Instance.Commit();

            // Recreate (with the same Guid and the same parent dictionary service)
            dbTsr = DBTickSizeRule.CreateDBTickSizeRule(tsr.ID, tsr.Name);
            dbTsr.Name = tsr.Name;
            dbTsr.DBDictionaryService = dbDictionaryService;

            // Add neded Fields
            foreach (var tsrPrice in tsr.PriceTable)
            {
                var dbTsrField = DBTickSizeRuleField.CreateDBTickSizeRuleField(Guid.NewGuid());
                dbTsrField.Price = tsrPrice;
                dbTsrField.TickSize = tsr.Tick(tsrPrice);
                dbTsr.DBTickSizeRuleField.Add(dbTsrField);
            }

            // Commit
            Singleton<DB>.Instance.Entities.AddToDBTickSizeRule(dbTsr);
            var n = Singleton<DB>.Instance.Commit();
            return n > 0;
        }

        #endregion
    }
}